export default class ObjectUtils {
  //克隆对象
  public static clone(source: object, deep: boolean = false): object {
    //值对象 直接返回
    if (typeof source != "object") {
      return source;
    }
    if (deep) {
      //JSON反序列化
      return JSON.parse(JSON.stringify(source));
    } else {
      var target: object;
      //是否是数组
      if (source instanceof Array) {
        target = [];
      } else {
        target = {};
      }
      for (var key in source) {
        target[key] = source[key];
      }
      return target;
    }
  }
  //返回对象的键列表
  public static getKeys(obj: object): Array<string> {
    var keys: Array<string> = [];
    for (var key in obj) {
      keys.push(key);
    }
    return keys;
  }
  //返回对象的值列表
  public static getValues(obj: object): Array<any> {
    var arr: Array<any> = [];
    for (var key in obj) {
      arr.push(obj[key]);
    }
    return arr;
  }
  //根据相同键覆盖
  public static cover(sources: object, target: object, needContainsKey: Boolean): void {
    if (!target || !sources) return;
    for (var key in target) {
      if (needContainsKey) {
        if (!sources.hasOwnProperty(key)) {
          continue;
        }
      }
      sources[key] = target[key];
    }
  }
  //获取对象键长度
  public static getKeyLen(obj: object): number {
    return ObjectUtils.getKeys(obj).length;
  }
  //获取键的最大值
  public static getMaxKeyValue(obj: object): number {
    var value: number = 0;
    for (var key in obj) {
      if (value < Number(key)) {
        value = Number(key);
      }
    }
    return value;
  }
}
